﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace NDK.Utils
{
    public class SerializeUtils
    {
        /// <summary>
        /// XML序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        public static void XmlSerialize<T>(string path, T obj)
        {
            if (obj == null)
            {
                return;
            }
            using (FileStream fileStream = new FileStream(path, FileMode.Create))
            {
                XmlSerializer xs = new XmlSerializer(obj.GetType());
                xs.Serialize(fileStream, obj);
            }
        } /// <summary>
          /// XML反序列化
          /// </summary>
          /// <typeparam name="T"></typeparam>
          /// <param name="path"></param>
          /// <returns></returns>
        public static T XmlDeserialize<T>(string path)
        {
            if (!File.Exists(path)) return default;

            using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                XmlSerializer xs = new XmlSerializer(typeof(T));
                T obj = (T)xs.Deserialize(fileStream);
                return obj;
            }
        }
        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="obj"></param>
        public static void BinarySerialize<T>(string path, object obj)
        {
            if (obj == null)
            {
                return;
            }
            FileUtils.DirExists(path);

            using (FileStream fileStream = new FileStream(path, FileMode.Create))
            {
                BinaryFormatter b = new BinaryFormatter();
                b.Serialize(fileStream, obj);
            }

        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static object BinaryDeserialize<T>(string path)
        {
            if (!File.Exists(path)) return new List<T>();

            using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                BinaryFormatter b = new BinaryFormatter();
                return b.Deserialize(fileStream);
            }
        }

        /// <summary>
        /// Json序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="list"></param>
        public static void JsonSerialize<T>(string path, List<T> list)
        {
            if (list == null)
            {
                return;
            }
            string json = JsonConvert.SerializeObject(list);

            FileUtils.WriteText(path, json);
        }
        public static void JsonSerialize(string path, object obj)
        {
            if (obj == null)
            {
                return;
            }
            string json = JsonConvert.SerializeObject(obj);

            FileUtils.WriteText(path, json);
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<T> JsonDeserializeList<T>(string path)
        {
            if (!File.Exists(path)) return new List<T>();

            string json = FileUtils.ReadAllText(path);
            List<T> list = JsonConvert.DeserializeObject<List<T>>(json);
            return list;
        }
        public static T JsonDeserialize<T>(string path)
        {
            if (!File.Exists(path)) return default;

            string json = FileUtils.ReadAllText(path);
            T obj = JsonConvert.DeserializeObject<T>(json);
            return obj;
        }
    }
}
